android versions

Biggs' Apps

for Android devices

  • android home
  • apps
  • testing
  • rants (main)
  • about biggs
  • crossroads

Kicking and Screaming

My Descent into Kotlin

My second kotlin rant


August 27, 2020

null pointers and lateinit

Probably the single biggest selling point of kotlin is that it is null-safe. You can't find a single article that doesn't talk about how kotlin will save you from the billion-dollar mistake in the first paragraph.

What they mean is that null-pointer errors plague java programs, and they have at times cost the developer a lot of money. Perhaps a billion dollars. Yes, this is very alarmist advertising and quite misleading. Anyone with any sense should easily recognize this as hyperbole.

So kotlin goes to extraordinary efforts to deal with null pointers. Every single type is declared as nullable or not. And if it is, then you have a huge slew of new operators that (supposedly) help deal with these possible nulls.

My experience has been that these are mostly a huge waste of time. Sure the Elvis operator is cool and useful, but most of the time these things just get in the way and greatly increase the time I spend writing code. In the old days I'd do a null check and then type away happily. Not anymore, since a variable could become null at any time (due to threading) you gotta check for nulls constantly. And it can be very very frustrating just to get the compiler to stop screaming at you.

And then I discovered lateinit. It seemed to solve a lot of those problems. I could use this keyword to declare a variable to not be nullable, and then go along my merry way. Yay!

But wait a minute...uh...I uh...yeah, there's a problem. Definitely a problem here.

Lateinit allows you to have an uninitialized variable (translation: null) and use it as if it was not nullable. It's impossible for the compiler to know if it's been initialized before using it. Wait, isn't that the original null-pointer problem that caused the billion dollar mistake in the first place? Yes, yes it is.

So kotlin forces programmers to bend over backwards to handle nullable variables, and then it supplies a back-door that essentially takes away all that type safety that it rigidly imposed.

WTF?


"Android Evolution" created by Manu Cornet, http://www.bonkersworld.net. All else is copyright 2020 by Scott M. Biggs and Sleep Furiously Productions. Not that that means much these days.